import {
  verifierActivity,
  writeOff
} from '../../apis/user';
import {
  splicing,
  handleDate
} from '../../utils/util';
const app = getApp();

Page({
  data: {
    popupShow: false,
    details: {},
    orderNo: ''
  },

  onLoad(options) {
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      let orderNo = scene.substr(scene.indexOf('=') + 1);
      this.setData({
        orderNo: orderNo
      });
      this.getVerifierActivity(orderNo);
    };
  },

  getVerifierActivity(order) {
    wx.showLoading({
      title: '请稍候...',
      mask: true
    });
    verifierActivity(order).then(res => {
      wx.hideLoading();
      if (res.data.code === 200) {
        let {
          data
        } = res.data;
        data.startTime = handleDate(data.startTime);
        data.closeTime = handleDate(data.closeTime);
        this.setData({
          details: data,
          popupShow: true
        });
      };
    });
  },

  // 扫码
  scanCode() {
    wx.scanCode({
      onlyFromCamera: true,
      success: res => {
        let {
          path
        } = res;
        let scene = path.substr(path.search(/orderNo/));
        let orderNo = decodeURIComponent(scene).substr(decodeURIComponent(scene).indexOf('=') + 1);
        this.getVerifierActivity(orderNo);
        this.setData({
          orderNo: orderNo
        });
      }
    });
  },

  // 核销
  writeOff() {
    writeOff(this.data.orderNo).then(res => {
      if (res.data.code === 200) {
        wx.showToast({
          title: '核销成功',
          duration: 1500,
          icon: 'success',
          mask: true,
          success: res => {
            this.setData({
              'details.sceneStatus': 1
            });
            let timer = setTimeout(() => {
              this.setData({
                popupShow: false,
                details: {}
              });
              clearTimeout(timer);
            }, 1500);
          }
        });
      };
    });
  },

  // 核销记录
  writeOffRecord() {
    wx.navigateTo({
      url: '/pages/writeOffList/index',
    });
  },

  hidePopup() {
    this.setData({
      popupShow: false
    });
  }
})